/**
 * BusPrdTraceInfo管理初始化
 */
var TraceSearch = {
    busPrdTraceId:$("#busPrdTraceId").val(),
    traceAddress:""
};

/**
 * 删除BusPrdTraceInfo
 */
TraceSearch.traceSearch = function () {
    if (this.busPrdTraceId) {
        var url=Feng.ctxPath + "/busPrdTraceInfo/traceSearch?busPrdTraceId="+this.busPrdTraceId+"&traceAddress="+this.traceAddress;
        console.log(url);
        $.ajax({
            url:url,
            type:"get",
            dataType:"json",
            success:function(response){
                console.log(response);
                if(response.code==200)
                {
                    var data=response.body;
                    var busComPrdInfo=data.busComPrdInfo;
                    var busPrdTraceInfoList=data.busPrdTraceInfoList;
                    $("#producerAddress").text(busComPrdInfo.comAddress);
                    $("#comanyName").text(busComPrdInfo.comName);
                    $("#producerName").text(busComPrdInfo.comName);
                    $("#comanyName").attr("href",busComPrdInfo.comUrl);
                    $("#producerUrl").attr("href",busComPrdInfo.comUrl);
                    $("#producerUrl").text(busComPrdInfo.comUrl);
                    $("#comHotline").text(busComPrdInfo.comHotline);
                    $("#prdName").text(busComPrdInfo.prdName);
                    $("#productName").text(busComPrdInfo.prdName);
                    $("#prdGb").text(busComPrdInfo.prdGb);
                    $(".selLogo").attr("src",busComPrdInfo.comLogoFilePath);
                    var tableData="";
                    for(var i=0;i<busPrdTraceInfoList.length;i++)
                    {
                        var busPrdTrace=busPrdTraceInfoList[i];
                        if(i==0)
                        {
                            $("#selectNum").text(busPrdTrace.traceCount);
                            $("#firstTime").text(busPrdTrace.firstTraceTime);
                            $("#firstAddress").text(busPrdTrace.firstTraceAddress);

                            $("#selectNum").text(busPrdTrace.traceCount);
                            $("#selectNum").text(busPrdTrace.traceCount);
                        }
                        tableData+='<tr>' +
                                '<td class="ubb1">' +
                                    '<div class="td_l fontsize1">' +
                                        busPrdTrace.traceTime+
                                    '</div>' +
                                    '<div class="td_r"><img src="/static/img/icon_2.png"></div>' +
                                    '<div class="thisnum"><span>第'+busPrdTrace.curTraceCount+'次</span></div>' +
                                '</td>' +
                                '<td class="ubb2">' +
                                    '<div class="nowAddress">'+busPrdTrace.traceAddress+'</div>' +
                                '</td>' +
                            '</tr>';
                    }
                    $("#queryHistoryBody").html(tableData);
                }else {
                    $(".notfound").fadeIn();
                    $("#productBox").fadeOut();
                }
            },
            error:function(data) {
               console.log(data);
            }
        });
    }
};
/**
 * 获取banner
 */
TraceSearch.getBannerList = function () {
        var url=Feng.ctxPath + "/banner/list4Front?prdNo="+this.busPrdTraceId.substring(0,5);
        console.log(url);
        $.ajax({
            url:url,
            type:"get",
            dataType:"json",
            success:function(response){
                console.log(response);
                var data=response;
                var tableData="";
                for(var i=0;i<data.length;i++)
                {
                    var banner=data[i];
                    tableData+='<div class="item '+(i==0?'active':'')+'">'
                                    +'<img style="width: 100%" src="'+banner.bannerImgFilePath+'">'
                                +'</div>';
                    $(".carousel-indicators").append('<li data-target="#carousel-example-generic" data-slide-to="'+i+'"  '+(i==0?'class="active"':'')+'></li>')
                }
                $("#bannerBox").html(tableData);
                $('.carousel').carousel({
                    interval: 2000
                })
            },
            error:function(data) {
                console.log(data);
            }
        });
};
/**
 * 查询BusPrdTraceInfo列表
 */
TraceSearch.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    BusPrdTraceInfo.table.refresh({query: queryData});
};

$(function () {
    if(TraceSearch.busPrdTraceId)
    {
        TraceSearch.getBannerList();
        var map = new BMap.Map("allmap");
        var point = new BMap.Point(116.404, 39.915);
        map.centerAndZoom(point, 15);
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                console.log(r.address);
                TraceSearch.traceAddress=r.address?r.address.province+r.address.city+r.address.district+r.address.street:"未知区域";
                TraceSearch.traceSearch();
//		            alert('您的位置：'+r.point.lng+','+r.point.lat);
            }
            else {
                TraceSearch.traceAddress="未知区域";
                TraceSearch.traceSearch();
                alert('failed'+this.getStatus());
            }
        },{enableHighAccuracy: true});
    }


});
